//
//  ZSPictureViewCell.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/16.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit

/**
 *  声明协议
 */
@objc protocol ZSPictureSelectorCellDelegate : NSObjectProtocol{

    optional func userWillChoosePicture()
    
    optional func userWillDeletePicture(cell:ZSPictureSelectorCell)

}



class ZSPictureSelectorCell: UICollectionViewCell {
   
    //添加一个图片属性
    var image:UIImage?{
    
        didSet{
        
btn.setImage(image, forState: .Normal)
        }
    }

    
    //添加一个代理属性
    var delegate:ZSPictureSelectorCellDelegate?
    
    /// 懒加载子控件-添加按钮
    lazy var btn:UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: .Normal)

        btn.sizeToFit()
    
        btn.addTarget(self, action: "btnAddDidClick", forControlEvents: .TouchUpInside)
    
        btn.imageView?.contentMode = .ScaleAspectFill
        
    return btn
        
    }()
    
    
    
    /**
     点解了添加按钮,利用代理进行操作
     */
   @objc private func btnAddDidClick(){
    
        print("点击了添加图片的按钮")
        delegate?.userWillChoosePicture?()

    }

    
    
    /// 懒加载子控件-删除按钮
    lazy var btnDelete:UIButton={
    
    let btn = UIButton()
    
        btn.setBackgroundImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: "btnDeleteDidClick", forControlEvents: .TouchUpInside)

    return btn
    }()
    
    /**点击了删除按钮*/
    @objc func btnDeleteDidClick(){
   
        print("点击了删除按钮")
    
        delegate?.userWillDeletePicture?(self)
    
    }
    
    
    
    
    
    
    
    
    func setupUI(){

    contentView.addSubview(btn)
    contentView.addSubview(btnDelete)
        
    btn.snp_makeConstraints { (make) -> Void in
        make.left.right.top.bottom.equalTo(contentView)
        }
    btnDelete.snp_makeConstraints { (make) -> Void in
        make.top.right.equalTo(contentView)
        }
        
    }
    
    /**重写初始化方法*/
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}





// MARK: - 类扩展中执行代理方法等逻辑
extension ZSPictureSelectorCell{


}
